package com.gw.booking.controller.wechat;

import com.gw.booking.common.MessageCode;
import com.gw.booking.common.ResultObject;
import com.gw.booking.common.Util;
import com.gw.booking.dto.PageDto;
import com.gw.booking.dto.backend.OrderDetailDto;
import com.gw.booking.dto.wechat.*;
import com.gw.booking.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.lang.reflect.InvocationTargetException;

@Api(tags = {"微信-订单管理"})
@Controller
@RequestMapping("/wechat/orders")
public class WOrderController {
    @Autowired
    private OrderService orderService;

    @ApiOperation(value = "订单列表（客户）")
    @RequestMapping(value = "/orderList.do", method = {RequestMethod.GET})
    @ResponseBody
    public ResultObject<PageDto<WOrderListDtoOut>> orderList(HttpServletRequest request
            , @ApiParam(value = "订单状态:全部,0预约确认中,1预约完成,2到店,3取消,4却下,5已超时",
            allowableValues = "0,1,2,3,4,5") @RequestParam(required = false) Integer state
            , @ApiParam(value = "页数", required = true) @RequestParam int page
            , @ApiParam(value = "数量", required = true) @RequestParam int size)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        return new ResultObject(MessageCode.CODE_SUCCESS, orderService.listForDiner(Util.getLoginToken(request), state, page, size));
    }

    @ApiOperation(value = "订单详情（客戶）")
    @RequestMapping(value = "/orderDetail.do", method = {RequestMethod.GET})
    @ResponseBody
    public ResultObject<WOrderDetailDtoOut> orderDetail(HttpServletRequest request
            , @ApiParam(value = "订单id", required = true) @RequestParam Integer id)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        return new ResultObject(MessageCode.CODE_SUCCESS, orderService.detailForDiner(Util.getLoginToken(request), id));
    }

    @ApiOperation(value = "取消订单（客戶）")
    @RequestMapping(value = "/{orderId}/cancel.do", method = {RequestMethod.PUT})
    @ResponseBody
    public ResultObject<WOrderDetailDtoOut> cancel(HttpServletRequest request
            , @ApiParam(value = "订单id", required = true) @PathVariable Integer orderId)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        orderService.txUpdateByDiner(Util.getLoginToken(request), orderId);
        return new ResultObject(MessageCode.CODE_SUCCESS, null);
    }

    @ApiOperation(value = "编辑订单（客户）")
    @RequestMapping(value = "/changeOrder.do", method = {RequestMethod.POST})
    @ResponseBody
    public ResultObject changeOrder(HttpServletRequest request
            , @Valid @RequestBody WChangeOrderDto wChangeOrderDto
            , BindingResult errors)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        if (errors.hasErrors()) {
            return new ResultObject(MessageCode.CODE_PARAMETER_ERROR, null, errors.getAllErrors());
        }
        orderService.txChange(Util.getLoginToken(request), wChangeOrderDto);
        return new ResultObject(MessageCode.CODE_SUCCESS, null);
    }

    @ApiOperation(value = "新增订单（客户）")
    @RequestMapping(value = "/addOrder.do", method = {RequestMethod.POST})
    @ResponseBody
    public ResultObject addOrder(HttpServletRequest request
            , @Valid @RequestBody WOrderDto wOrderDto
            , BindingResult errors)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        if (errors.hasErrors()) {
            return new ResultObject(MessageCode.CODE_PARAMETER_ERROR, null, errors.getAllErrors());
        }
        orderService.txAdd(Util.getLoginToken(request), wOrderDto);
        return new ResultObject(MessageCode.CODE_SUCCESS, null);
    }
}
